package com.osfw.framework.exception;


import com.osfw.framework.utils.MessageUtils;
import com.osfw.framework.utils.StringUtils;

/**
 * 用户信息异常类
 */
public class UserException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    /**
     * 所属模块
     */
    private String module;

    /**
     * 错误码
     */
    private String code;

    /**
     * 错误码对应的参数
     */
    private Object[] args;

    /**
     * 错误消息
     */
    private String defaultMessage;

    public UserException(String module, String code, Object[] args, String defaultMessage) {
        this.module = module;
        this.code = code;
        this.args = args;
        this.defaultMessage = defaultMessage;
    }

    public UserException(String module, String code, Object[] args) {
        this(module, code, args, null);
    }

    public UserException(String module, String defaultMessage) {
        this(module, null, null, defaultMessage);
    }

    public UserException(String defaultMessage) {
        this(null, null, null, defaultMessage);
    }

    public UserException(String code, Object[] args) {
        this("user", code, args, null);
    }

    public static void throwUserException(String code, Object[] args) {
        throw new UserException("user", code, args, null);
    }

    //黑名单IP异常类
    public static void throwUnauthUNAUTHORIZED_NONE() {
        throwUserException("login.blocked", null);
    }

    //黑名单IP异常类
    public static void throwBlackListException() {
        throwUserException("login.blocked", null);
    }

    //用户锁定异常类
    public static void throwUserBlockedException() {
        throwUserException("user.blocked", null);
    }

    //用户账号已被删除
    public static void throwUserDeleteException() {
        throwUserException("user.password.delete", null);
    }

    //用户不存在异常类
    public static void throwUserNotExistsException() {
        throwUserException("user.not.exists", null);
    }

    //用户密码不正确或不符合规范异常类
    public static void throwUserPasswordNotMatchException() {
        throwUserException("user.password.not.match", null);
    }

    //用户错误记数异常类
    public static void throwUserPasswordRetryLimitCountException(int retryLimitCount) {
        throwUserException("user.password.retry.limit.count", new Object[]{retryLimitCount});
    }

    //用户错误最大次数异常类
    public static void throwUserPasswordRetryLimitExceedException(int retryLimitCount) {
        throwUserException("user.password.retry.limit.exceed", new Object[]{retryLimitCount});
    }


    @Override
    public String getMessage() {
        String message = null;
        if (!StringUtils.isEmpty(code)) {
            message = MessageUtils.message(code, args);
        }
        if (message == null) {
            message = defaultMessage;
        }
        return message;
    }

    public String getModule() {
        return module;
    }

    public String getCode() {
        return code;
    }

    public Object[] getArgs() {
        return args;
    }

    public String getDefaultMessage() {
        return defaultMessage;
    }

}
